const FirstShowInfo = [{ "image": "https://th.bing.com/th/id/OIP.duz6S7Fvygrqd6Yj_DcXAQHaF7?w=211&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7", "owner": "WPF" },
{ "image": "https://th.bing.com/th/id/OIP.duz6S7Fvygrqd6Yj_DcXAQHaF7?w=211&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7", "owner": "LZS" },
{ "image": "https://th.bing.com/th/id/OIP.duz6S7Fvygrqd6Yj_DcXAQHaF7?w=211&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7", "owner": "WPF" },
{ "image": "https://th.bing.com/th/id/OIP.duz6S7Fvygrqd6Yj_DcXAQHaF7?w=211&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7", "owner": "LZS" }];


Page({
  // 页面数据
  data: {
    allGoodsInfo: [{ "image": "https://th.bing.com/th/id/OIP.duz6S7Fvygrqd6Yj_DcXAQHaF7?w=211&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7", "owner": "WPF" },
    { "image": "https://images.pexels.com/photos/18796528/pexels-photo-18796528.jpeg?auto=compress&cs=tinysrgb&w=600&lazy=load", "owner": "SHY" }],

    loading: false,

    CarouselPhoto: ['./images/1.jpg', './images/2.jpg'],  // 轮播图的图片数据
  },

  // 页面加载函数
  onLoad() {
    this.getGoodsData();
    this.requestToBackend_goodsList();
  },

  // 异步获取商品数据
  getGoodsData() {
    console.log("GET DATA...")

    // 获取当前已显示的数据
    const currentGoods = this.data.allGoodsInfo.slice();

    // 更新数据并合并新加载的数据
    const updatedGoods = currentGoods.concat(FirstShowInfo);

    // 更新页面显示
    this.setData({
      allGoodsInfo: updatedGoods,
    });
  },

  // 切换到二手市场界面
  switchToMarket() {
    my.navigateTo({
      url: '../../pages/second_Shop/second_Shop'
    });
  },

  // 切换到跑腿界面
  switchToRunning() {
    my.navigateTo({
      url: '../../pages/run_Man/run_Man'
    });
  },
  
  // product_list
  requestToBackend_goodsList(){
    const that = this;
    // const apiUrl = 'https://localhost:3000/php/1.php'; // 替换成你的后端接口地址
    // const apiUrl = 'http://172.20.55.210:10086/product/getproducts'; // 替换成你的后端接口地址
    const apiUrl = 'http://172.20.55.210:10086/product'; // 替换成你的后端接口地址

    my.request({
      url: apiUrl,
      method: 'GET',
      // data: data，
      dataType: 'json',
      success: resp => {
        console.log(resp.data);
        allGoodsInfo = resp.data;
        my.alert({ content: 'success' });
      },
      fail: error => {
        console.error('请求失败：', error);
        my.alert({ content: '请求失败，详细错误信息请查看控制台' });
      },
    });
  }

});
